<?php

/**
 * NOTICE OF LICENSE
 *
 * THIS SOURCE FILE IS PART OF EVEBIT'S PRIVATE PROJECT.
 * 
 * DO NOT USE THIS FILE IN OTHER PLACE.
 *
 * @category    ChinaWanderer
 * @package     Backend
 * @subpackage  Controller
 * @author      Chen Qiao <chen.qiao@evebit.com>
 * @version     $$Id: IndexController.php 1062 2011-05-26 10:52:52Z chen.qiao $$
 * @copyright   Copyright (c) 2008 Evebit Inc. China (http://www.evebit.com)
 */

/**
 * Default controller for backend
 * 
 * @package     Backend
 * @subpackage  Controller
 * @author      Chen Qiao <chen.qiao@evebit.com>
 * @version     $$Id: IndexController.php 1062 2011-05-26 10:52:52Z chen.qiao $$
 */

class Default_IndexController extends Evebit_Controller_Action_Backend {
	
	/**
	 * default action of the index controller
	 * 
	 */
	public function indexAction() {
	}
	/**
	 * login action of the index controller
	 * 
	 * login authentication
	 * 
	 */
	public function loginAction() {
		$adapter = $this->getInvokeArg ( 'bootstrap' )->getResource ( 'auth' )->getAdapter ();
		
		$checkCode = $this->getRequest ()->getParam ( 'checkcode' );
		$token = $this->getRequest ()->getParam ( 'token' );
		$identify = $this->getRequest ()->getParam ( $adapter->getOption ( 'identify_field' ) );
		$password = $this->getRequest ()->getParam ( $adapter->getOption ( 'password_field' ) );
		
		if (!$identify){
			return ;
		}
		
		$adapter->setIdentity ( $identify );
		$adapter->setPassword ( $password );
		$adapter->setCheckCode ( $checkCode );
		$adapter->setToken ( $token );
		$adapter->setIdentityField ('username');
		$adapter->setIsGoogleLogin(false);

		$auth = Zend_Auth::getInstance ();
		$result = $auth->authenticate ( $adapter );
		
		if ($result->isValid ()) {
			$this->getSession ()->addSuccess ( "You have logged in" );
			$this->_redirect ( '/index/index' );
		} else {
			$resultcaptchaurltoken = $result->getIdentity ();
			if (empty ( $resultcaptchaurltoken ['url'] ) || empty ( $resultcaptchaurltoken ['token'] )) {
				$this->view->resultcaptchaurltoken = $resultcaptchaurltoken;
			}
			$this->getSession ()->addError ( implode ( '<br/>', $result->getMessages () ) );
		}
	}
	
	/**
	 * logout action of the index controller
	 * 
	 * logout authentication
	 * 
	 */
	public function logoutAction() {
		Zend_Auth::getInstance ()->clearIdentity ();
		$this->getSession ()->addSuccess ( $this->__ ( "Logout Successfully" ) );
		$this->_redirect ( "/login" );
	}
	
	/**
	 * change locale
	 */
	public function localeAction() {
		/* @var $translate Zend_Translate_Adapter_Csv */
		$locale = $this->getRequest ()->getParam ( 'lang' );
		$return = $this->getRequest ()->getParam ( 'reun' );
		
		$translate = Zend_Registry::get ( 'Zend_Translate' );
		
		if ($translate->isAvailable ( $locale )) {
			$this->getSession ()->setLocale ( $locale );
		} else {
			$this->getSession ()->addError ( $this->__ ( "Locale %s not valid", $locale ) );
		}
		
		if ($return) {
			$return = base64_decode ( $return );
		}
		if (! $return) {
			$return = '/';
		}
		
		$this->_redirect ( $return );
	}

}

